﻿/*
 * Created by SharpDevelop.
 * User: Hellen(LongFan)
 * Date: 2015-11-23
 * Time: 19:09
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;			

namespace Comm
{
	/// <summary>
	/// Description of Form1.
	/// </summary>
	//declare delegate to deal button pushed
	public delegate void MyConfigCommEventHandler(object sender, EventArgs e);
	
	public partial class ConfigForm : Form
	{
		//declare a event to send message
		public event MyConfigCommEventHandler ConfCommEvent;
		
		public ConfigForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			if( SerialPort.GetPortNames() == null )
			{	//check if have comm port
				MessageBox.Show("本机没有串口！");
				return ;
			}
			commNo.Items.AddRange(SerialPort.GetPortNames());		//add comm port name
			if( MyCommClass.SPortName == null )
			{
				//if opend the form first, Set the default Paramate
				commNo.SelectedIndex = 0;
				comBaudRate.SelectedIndex = 9;
				comDataBit.SelectedIndex = 1;
				comParity.SelectedIndex = 0;
				comStopBit.SelectedIndex = 0;				
			}
			else
			{
				commNo.Text = MyCommClass.SPortName;
				comBaudRate.Text = Convert.ToString(MyCommClass.SBaudRate);
				comDataBit.Text = Convert.ToString(MyCommClass.SByteSize);
				comStopBit.SelectedIndex = MyCommClass.SStopBits;
				comParity.SelectedIndex = MyCommClass.SParity;
			}
			ComBufferComboBox.SelectedIndex = 12;	//set comm soft buf 1MByte
		}
		
		void CommNoSelectedIndexChanged(object sender, EventArgs e)
		{
			
		}
		
		void Label1Click(object sender, EventArgs e)
		{
			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			//错误处理，没有选择串口和波特率
			if(commNo.Text == "" || comBaudRate.Text == "")
			{
				MessageBox.Show("串口号和波特率必须选择！");
				return ;
			}
			//
			MyCommClass.SPortName = commNo.Text;
			MyCommClass.SBaudRate = Convert.ToInt32(comBaudRate.Text);
			MyCommClass.SByteSize = Convert.ToByte(comDataBit.Text);
			if(comStopBit.SelectedItem == null)
			{
				MyCommClass.SParity = 0;
			}else
			{
				MyCommClass.SParity = Convert.ToByte(comParity.SelectedIndex);
			}
			
			if(comStopBit.SelectedItem == null)
			{
				MyCommClass.SStopBits = 0;
			}else
			{
				MyCommClass.SStopBits = Convert.ToByte(comStopBit.SelectedIndex);
			}
			
			ConfCommEvent(this,e);
			
			this.Close();
		}
		
		void Label5DoubleClick(object sender, EventArgs e)
		{
			comDataBit.Enabled = true;
			comParity.Enabled = true;
			comStopBit.Enabled = true;	
			ComBufferComboBox.Enabled = true;			
		}
		
		void ConfigFormLoad(object sender, EventArgs e)
		{	//setting the tooltips
			toolTip1.SetToolTip(AdvLabel,"双击解锁高级功能");
			if(!comParity.Enabled)
			{
				toolTip1.SetToolTip(comParity, "双击\"Advance\"解锁高级功能");
				toolTip1.SetToolTip(ParityLabel, "双击\"Advance\"解锁高级功能");
			}
			if(!comStopBit.Enabled)
			{
				toolTip1.SetToolTip(comStopBit, "双击\"Advance\"解锁高级功能");
				toolTip1.SetToolTip(StopBitLabel, "双击\"Advance\"解锁高级功能");
			}
			if(!ComBufferComboBox.Enabled)
			{
				toolTip1.SetToolTip(ComBufferComboBox, "双击\"Advance\"解锁高级功能");
				toolTip1.SetToolTip(ReadBufLabel, "双击\"Advance\"解锁高级功能");
			}
			if(!comDataBit.Enabled)
			{
				toolTip1.SetToolTip(comDataBit, "双击\"Advance\"解锁高级功能");
				toolTip1.SetToolTip(DataBitLabel, "双击\"Advance\"解锁高级功能");
			}
		}
	}
}
